package com.vmock.base.core.url;

import com.vmock.base.utils.SpringUtils;
import com.vmock.biz.entity.Url;
import com.vmock.biz.enums.ResponseTypeEnum;
import com.vmock.biz.service.IUrlService;
import lombok.SneakyThrows;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PrefixUrlHandler extends BaseMockUrlHandler {

    /**
     * url service
     */
    private IUrlService mockUrlService = SpringUtils.getBean(IUrlService.class);

    @Override
    protected Url find(String requestUrl) {
        List<Url> urls = mockUrlService.listByRespType(ResponseTypeEnum.PREFIX_PROXY);
        for (Url url : urls) {
            Pattern pattern = Pattern.compile(url.getUrl());
            Matcher matcher = pattern.matcher(requestUrl);
            if (matcher.matches()) {
                return url;
            }
        }
        return null;
    }
}
